package com.serookie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serookie.dto.OrderGoods;
import com.serookie.entity.*;
import com.serookie.mapper.*;
import com.serookie.service.GoodsService;
import com.serookie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kevintam
 * @version 1.0
 * @date 2021/5/31 19:23
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService{

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Override
    public List<Order> all() {
        List<Order> orders = orderMapper.selectList(null);
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        orders.forEach(element->{
            Address address = addressMapper.selectById(element.getAddressId());
            element.setAddresses(address);
            wrapper.eq("openid",element.getOpenid());
            SysUser sysUser = sysUserMapper.selectOne(wrapper);
            element.setNickName(sysUser.getName());
        });
        return orders;
    }

    @Override
    public Boolean addOrder(Order order) {
        int insert = orderMapper.insert(order);
        Integer orderId = order.getOrderId();
        QueryWrapper<OrderDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        List<OrderDetails> orderDetails = orderDetailsMapper.selectList(wrapper);
        Integer count=0;
        orderDetails.forEach(v->{
            Goods goods = goodsMapper.selectById(v.getGoodsId());
            goods.setGoodsNumber(goods.getGoodsNumber()-1);
            goodsMapper.updateById(goods);
        });
        return insert>0;
    }

    @Override
    public Order OrderByNumber(String  Number) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_number",Number);
        return orderMapper.selectOne(wrapper);
    }

    @Override
    public List<Order> getOrder(String orderNumber) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        QueryWrapper<SysUser> wrapper1 = new QueryWrapper<>();
        wrapper.like("order_number",orderNumber);
        List<Order> orders = orderMapper.selectList(wrapper);
        orders.forEach(element->{
            Address address = addressMapper.selectById(element.getAddressId());
            element.setAddresses(address);
            wrapper1.eq("openid",element.getOpenid());
            SysUser sysUser = sysUserMapper.selectOne(wrapper1);
            element.setNickName(sysUser.getName());
        });
        return orders;

    }

    @Override
    public List<Order> OrderByUserId(Integer userId) {
        QueryWrapper<Order>  wrapper = new QueryWrapper<>();
        wrapper.like("user_id",userId);
        return orderMapper.selectList(wrapper);
    }
    //根据订单状态查询订单
    @Override
    public List<Order> getPayStatus(Integer payStatus) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_status",payStatus);
        return orderMapper.selectList(wrapper);
    }

    @Override
    public Boolean deleteOrder(Integer orderId) {
        return orderMapper.deleteById(orderId)>0;
    }

    @Override
    public Boolean updateOrder(Order order) {
        return orderMapper.updateById(order)>0;
    }

    @Override
    public Integer goodsCount() {
        return orderMapper.selectCount(null);
    }
}
